import 'package:boss/provider/takeout_process_provider.dart';
import 'package:boss/ui/widget/alert/alert.dart';
import 'package:boss/ui/widget/form/long_text_form_field.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class RefundUtil {
  static agree(
    BuildContext context, {
    String takeOutId,
    String refundId,
    int price,
    int status,
    int backDeliveryType, //非必传 只有异常单外卖传
  }) async {
    var provider = Provider.of<TakeoutProcessProvider>(
      context,
      listen: false,
    );
    showAlert(
      context,
      content: '确认退款实付金额${AmountUtil.price2String(price)}元给客户吗?',
      confirmText: '确认',
      cancelText: '取消',
      onConfirm: (context) async {
        if (await provider.agreeRefund(
          takeOutId: takeOutId,
          refundId: refundId,
          status: status,
          backDeliveryType: backDeliveryType,
        )) {
          Navigator.pop(context);
        }
      },
      onCancel: (context) {
        Navigator.pop(context);
      },
    );
  }

  static reject(
    BuildContext context, {
    String takeOutId,
    String refundId,
    int status,
  }) async {
    var provider = Provider.of<TakeoutProcessProvider>(
      context,
      listen: false,
    );
    var reason = '';

    showAlert(
      context,
      content: LongTextFormField(
        maxLength: 220,
        maxLines: 4,
        hintText: '请输入拒绝退款原因',
        onChanged: (value) {
          reason = value;
        },
      ),
      onConfirm: (context) async {
        if (await provider.rejectRefund(
          takeOutId: takeOutId,
          reason: reason,
          refundId: refundId,
          status: status,
        )) {
          Navigator.pop(context);
        }
      },
      onCancel: (context) {
        Navigator.pop(context);
      },
    );
  }
}
